﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.CMS263.Util
{
    public class tvPage
    {
        /// <summary>
        /// 上一页下一页通用方法
        /// </summary>
        /// <param name="count">信息总条数</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="CurrentIndex">当前第几页</param>
        /// <returns></returns>
        public static string GetNumPager(int pagelength, int count, int pagesize, int CurrentIndex, string urlkeys)
        {
            StringBuilder sb = new StringBuilder();
            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);
            int middleCount = (pagelength % 2 == 0 ? pagelength / 2 : pagelength / 2 + 1);
            if (CurrentIndex > 1)
            {
                sb.Append("<a href='?pageTv=" + (CurrentIndex - 1) + urlkeys + "' class='prev'><< 上一页</a>");
            }
            else
            {
                sb.Append("<a class='prev'><< 上一页</a>");
            }
            //分页头
            if (pageCount <= pagelength)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    if (CurrentIndex == i)
                    { sb.Append("<a href='?pageTv=" + i + urlkeys + "' class='now'>" + i + "</a>"); }
                    else
                    { sb.Append("<a href='?pageTv=" + i + urlkeys + "'>" + i + "</a>"); }
                }
            }
            else
            {
                if (CurrentIndex <= middleCount)
                {
                    for (int i = 1; i <= pagelength; i++)
                    {
                        if (CurrentIndex == i)
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "' class='now'>" + i + "</a>"); }
                        else
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "'>" + i + "</a>"); }
                    }
                }
                else if (CurrentIndex >= (pageCount - middleCount))
                {
                    for (int i = pageCount - pagelength + 1; i <= pageCount; i++)
                    {
                        if (CurrentIndex == i)
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "' class='now'>" + i + "</a>"); }
                        else
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "'>" + i + "</a>"); }
                    }
                }
                else
                {
                    int first = CurrentIndex - (pagelength - middleCount);
                    for (int i = first; i < first + pagelength; i++)
                    {
                        if (CurrentIndex == i)
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "' class='now'>" + i + "</a>"); }
                        else
                        { sb.Append("<a href='?pageTv=" + i + urlkeys + "'>" + i + "</a>"); }
                    }
                }
            }
            if (CurrentIndex < pageCount)
            {
                sb.Append("<a href='?pageTv=" + (CurrentIndex + 1) + urlkeys + "' class='prev'> >>下一页</a>");
            }
            else
            {
                sb.Append("<a class='prev'> >>下一页</a>");
            }
            //分页尾
            return sb.ToString();
        }

        public static string GetPageNew(int count, int pagesize, int CurrentIndex, string type,string page)
        {

            StringBuilder sb = new StringBuilder();

            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);
            if (count > 0)
            {
                sb.Append("<span>第&nbsp;" + CurrentIndex + "/" + pageCount + "&nbsp;页;每页&nbsp;" + pagesize + "&nbsp;条;共&nbsp;" + count + "&nbsp;条记录&nbsp;&nbsp;&nbsp;&nbsp;");
                if (pageCount == 1)
                {
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input type='text' class='input_text' size='4' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else if (CurrentIndex == 1)
                {
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl("+page+")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" +page+"="+ (CurrentIndex + 1) + type + "'><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + pageCount + type + "'><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else if (CurrentIndex == pageCount)
                {
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=1" + type + "'><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex - 1) + type + "'><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl(" + page + ")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else
                {
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=1" + type + "'><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex - 1) + type + "'><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl(" + page + ")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex + 1) + type + "'><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + pageCount + type + "'><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
            }
            else
            {
                sb.Append("<span>抱歉,没有找到相关数据</span>");
            }
            return sb.ToString();
        }
    }   
}
